<?php 
/****************************	BEGIN: DEBUG HELPER ***************************/
if ( ! function_exists('last_query'))
{
	function last_query($exit=false)
	{
		$CI =& get_instance();
		echo $CI ->db->last_query();
		if($exit){
			exit();
		}
	}
}

if ( ! function_exists('pr'))
{
	function pr($item, $exit=false)
	{
		$output = '';
		
		ob_start();
		echo "<pre/>";
		if(is_array($item) || is_object($item)){
			var_dump($item);
		} else {
			echo $item;
		}
		$output = ob_get_contents();
		ob_end_clean();
		
		echo $output;
		
		if($exit){
			exit();
		}
		return $output;
	}
}

if ( ! function_exists('var_dump_ret'))
{
	function var_dump_ret($mixed = null) {
	  ob_start();
	  var_dump($mixed);
	  $content = ob_get_contents();
	  ob_end_clean();
	  return $content;
	}
}
/****************************	END: DEBUG HELPER ***************************/

/*BEGIN: CGA HELPER (GET/POST )*/
function do_authenticate($url,$username,$password){
	$response = '';
	try {
		//Initialize a cURL session
		$curl = curl_init();
		//Return the transfer as a string of the return value of curl_exec() instead of outputting it out directly
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		//Set URL to fetch
		curl_setopt($curl, CURLOPT_URL, $url);
		//Force HTTP version 1.1
		curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
		//Use NTLM for HTTP authentication
		curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
		//Username:password to use for the connection
		curl_setopt($curl, CURLOPT_USERPWD, $username . ':' . $password);
		//Stop cURL from verifying the peer’s certification
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		//Execute cURL session
		$response = curl_exec($curl);
		//Close cURL session
		curl_close($curl);
	} catch (Exception $e){
		//echo $e->getMessage();
	}
	return $response;
}

function cgate_setup_curl($url){
	$ch = curl_init ($url);
	//curl_setopt ($ch, CURLOPT_MUTE, 1);
	curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Content-Type: text/xml'));
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	return $ch;
}

function cgate_post_xml($url,$xml){
	$response  = '';
	try {
		if(!empty($url) && !empty($xml)){
			$ch = cgate_setup_curl($url);
			curl_setopt ($ch, CURLOPT_POST, 1);
			curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Content-Type: text/xml'));
			curl_setopt ($ch, CURLOPT_POSTFIELDS, "$xml");
			$response = curl_exec ($ch);
			curl_close ($ch);
		}
	} catch (Exception $e){
		//echo $e->getMessage();
	}
	return $response;
}

function cgate_get($url,$params){
	$response  = '';
	try {
		if(!empty($url)){
			if(!empty($params) && is_array($params)){
				$url .= '?';
				foreach($params as $key=>$value){
					$key = urldecode($key);
					$value = urldecode($value);
					$url .= "{$key}={$value}&";
				}
				$url = substr($url,0,-1);//pr($url,1);
			}
			$ch = cgate_setup_curl($url);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
			$response = curl_exec ($ch);
			curl_close ($ch);
		}
	} catch (Exception $e){
		//echo $e->getMessage();
	}
	return $response;
}
/*END: CGA HELPER (GET/POST )*/